function parseNum(val, scale) {
			try{
				if(val.indexOf(',') >=0)val = val.replace(/,/g,"");
			}catch(e){}
		    
		    if (isNaN(val) == true || val == undefined || val == '') {
		        return 0;
		    }
		    // 不设的话默认不带小数
		    if (scale == null || scale == undefined) {
		        scale = 2;
		    }
		    val = parseFloatExtension(val);

		    return parseFloatExtension(Math.round(val * Math.pow(10,scale)) / Math.pow(10,scale));
		}

		function parseFloatExtension(val){
		    return Math.round(parseFloat(val) * Math.pow(10,12))/Math.pow(10,12);
		}

		//计算收益结果 
		/*
			data 销售及成本
			copY 合作年限
			others 其他收入
			equipcosts 仪器成本 
			feeRito 销售费用比例
			accDate 客户帐期
			developFee 开发费用
			
		*/
		function calcResult(data , copY, others,equipcosts , accDate,developFee,type  ){

			var resdata = {
				sum:[],
				data:[],
			}
			var res  = [];
			var sum = {
				otherincome:0,
				equipcost:0,
				addtax:0,
				motionfee:0,
				moneycost:0,
				repaircost:0,
				servicefee:0,
				incometax:0,
				curprofit:0,
				curmoney:0,

			};

			var eqSum = 0 ;
			var sum5 = {
				equipcost:0,
				regentsale:0,
				curprofit:0,
				profitrito:0,
				backday:0,
				mkrito:0,

			};
			for(var i in equipcosts){

				eqSum += equipcosts[i]  / 10000 / (1+CONFIG.TAXRITO);
				if(copY > 5 && i < 5 ){
					sum5.equipcost += equipcosts[i] /10000 / (1+CONFIG.TAXRITO) ;
				}
			}
			//现金流计算
			for(var i = 0 ; i < copY ; i ++){
				//仪器成本*Math.pow(1.1, -(year - 1))
				 	if(i > 0){
				 		sum.curmoney += equipcosts[i]/(1+CONFIG.TAXRITO)  *Math.pow(1+CONFIG.NPVRITO,-i) / 10000 ;
				 	}else{
				 		sum.curmoney += equipcosts[i] / 10000 /  (1+CONFIG.TAXRITO) ;
				 	}
			}

			var curmoneys = new Array();
			var curmoneys5 = new Array();

			///销售费用比例
			var feeRito = data.data[0].feeRito / 100 ;

			for(var i = 0 ; i <= copY ; i ++){
				var d = {};
               // console.log("第"+(i+1)+"年");
				d.regentsale = data.sum["sale"+ i] /(1+CONFIG.TAXRITO)/ 10000;//试剂销售
				d.regentsaletax = data.sum["sale"+ i] / 10000;
				d.regentcost = data.sum["cost"+ i] /(1+CONFIG.TAXRITO) / 10000;//试剂成本
				d.mksale = data.sum["mksale"+i] /10000 ; 
				d.nomksale = data.sum["nomksale"+i] /10000;
				if(i != copY){

					d.year = "第"+(i+1)+"年";

					//其他收入
					d.otherincome = others[i]/(1+CONFIG.TAXRITO) / 10000;

					sum.otherincome += d.otherincome;
                    //console.log("otherincome");
					//仪器成本
					d.equipcost = equipcosts[i] / 10000 / (1+CONFIG.TAXRITO) ;
					sum.equipcost += d.equipcost;
					//console.log("equipcost");
					//增值税
					d.addtax = (d.regentsale + d.otherincome - d.equipcost - d.regentcost) * CONFIG.TAXRITO ;
					sum.addtax += d.addtax;
                   // console.log("addtax");
					//运营费用
					d.motionfee = d.regentsale * CONFIG.servicefeeRito ;
					sum.motionfee += d.motionfee ;
                   // console.log("motionfee");
					//资金成本
					d.moneycost =  type == CONFIG.sub ? (d.mksale  * accDate / 12 * CONFIG.moneycostRito +  d.nomksale  * accDate / 12 * CONFIG.notmkMoneycostRito ) : type == CONFIG.group ? d.regentsaletax * CONFIG.moneycostRito * accDate / 12:0 ;  
					sum.moneycost += d.moneycost;
                   // console.log("moneycost");
					//维修成本
					d.repaircost = sum.equipcost * CONFIG.repaircostRito 
					sum.repaircost += d.repaircost;
                   // console.log("repaircost");
					//业务费用
					d.servicefee = d.regentsale  * feeRito   / CONFIG.servicecostRito;
					if(i == 0 ) d.servicefee += developFee / CONFIG.servicecostRito / 10000  //第一年 加 开发费用
					sum.servicefee += d.servicefee;
                  //  console.log("servicefee");
					//所得税
					if(type == CONFIG.sub){
				 		d.incometax =( d.regentsale + d.otherincome - d.regentcost - eqSum/5 - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax *CONFIG.addtaxattachRito ) * CONFIG.incometaxRito;

					}else if(type == CONFIG.group){
				 		d.incometax =( d.regentsale + d.otherincome - d.regentcost - eqSum/5 - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax *CONFIG.addtaxattachRito ) *  (CONFIG.incometaxRito * (1- data.rito / 100 )  + CONFIG.mkincometaxRito  * data.rito/ 100 );
					}

				 	sum.incometax += d.incometax; 
                    //console.log("incometax");
				 	//当年利润
				 	d.curprofit = d.regentsale + d.otherincome - d.regentcost -  eqSum/5  - d.motionfee - d.moneycost - d.repaircost - d.servicefee  - d.addtax * CONFIG.addtaxattachRito - d.incometax ;
				 	sum.curprofit += d.curprofit;
                   // console.log("curprofit");
				 	//计算 以5年 标准
				 	if(copY > 5 && i < 5 ){
				 		sum5.regentsale += d.regentsale;
				 		sum5.curprofit += d.curprofit;

				 	}
				 	
				}else{
					d.year = "合计";
					d.otherincome = sum.otherincome ;

					d.equipcost = sum.equipcost;
					d.addtax = sum.addtax;
					d.motionfee = sum.motionfee;
					d.moneycost = sum.moneycost;
					d.repaircost = sum.repaircost;
					d.servicefee = sum.servicefee;
					d.incometax = sum.incometax;
					d.curprofit = sum.curprofit;


				}
				//当年现金流 = 仪器成本 + 当年利润
				if(i == 0) {
					d.curmoney = - sum.curmoney  * (1+ CONFIG.TAXRITO );
				}else{
					d.curmoney = (eqSum/5 * (1+ CONFIG.TAXRITO ) + res[i -1].curprofit)  ;
				}

				curmoneys.push(d.curmoney);
				if(copY > 5 && i < 5 )curmoneys5.push(d.curmoney);
				
				res.push(d);

			}


			//收益率
			//console.log(curmoneys);
			res[0].profitrito = IRR(curmoneys) * 100;

			if(copY > 5 )sum5.profitrito = IRR(curmoneys5) * 100;

			//回收期
			var mons = copY * 12 ; 
			var R = res[copY].equipcost / ( res[copY].equipcost +res[copY].curprofit ); 
			res[0].backday = calcBackDay(copY,R,mons);

			if(copY > 5){
				R = sum5.equipcost / (sum5.equipcost + sum5.curprofit );
				mons = 60 ; 
				sum5.backday = calcBackDay(5,R,mons);

			}


			resdata.data = res;
			if(copY > 5){
				resdata.sum.push({
					copY:  "5年参考",
					sale: sum5.regentsale,
					cost: sum5.equipcost,
					profit: sum5.curprofit,
					rito:sum5.profitrito,
					backday:sum5.backday,
					mkrito:data.rito,

				});
			}
			resdata.sum.push({
				copY: "本次"+copY + "年",
				sale: res[copY].regentsale,
				cost: res[copY].equipcost,
				profit: res[copY].curprofit,
				rito:res[0].profitrito,
				backday:res[0].backday,
				mkrito:data.rito,

			});

			
			return resdata; 

		}
		function calcBackDay(copY, R , mons ){
			mons = 60;
			return ( R * copY < 0 ) ? Math.round( ( R  *mons  - Math.floor( R  * mons )   ) * 12,0) +"个月" :  Math.floor( R*mons/12 ) +"年" +  Math.round( ( R  *mons  - Math.floor( R  * mons )   ) * 12,0) +"个月" ; 
		}
		////计算销售额及成本
    	function calcSaleAndCost(data, copY,incrRito,feeRito ,type ){
	
			var res = {

				data :[],
				sum :{},
				rito:0,
			}
			for(var i = 0 ; i <= copY ;  i ++){

				res.sum['sale'+i] = 0 ; 
				res.sum['cost'+i] = 0 ; 
				res.sum['mksale'+i] = 0 ; 
				res.sum['nomksale'+i] = 0 ; 

			}
			var mksale = 0;var sumsale= 0 ; 


			for(var i = 0 ; i < data.length ; i++){


				var d = data[i];
				d.index = (i + 1);

				d.feeRito  = feeRito ;

				d['sale'+copY] = 0 ; 
				d['cost'+copY] = 0 ; 
				d['mksale'+copY] = 0 ; 
				d['nomksale'+copY] = 0 ; 

				var isMk = d.brand == '迈克';
				d.groupgross  = isMk ? 70 + 0.3 * d.subgross : d.subgross ;
				if(d.brand == '迈克'){

					mksale +=d.estimatedSales;

				}
				sumsale+= d.estimatedSales;

			    for(var j = 0 ; j < copY ; j++){

					if( j == 0){

						d["sale"+j] = d.estimatedSales;//销售额
						
						d["cost"+j] = (d.estimatedSales == null || d.estimatedSales == 0 )? 0 : d.estimatedSales * (1 - (type == CONFIG.sub ? d.subgross : d.groupgross )   / 100 ) ;//成本
					 
					}else{

						d["sale"+j] = d["sale"+(j-1)] * (1+ incrRito[j]/ 100 ) ;//销售额
						d["cost"+j] = d["sale"+j] * (1 - (type == CONFIG.sub ? d.subgross : d.groupgross ) / 100) ;//成本
						
					}
					d["mksale"+j] = isMk ? d["sale"+j] : 0 ; 
					d["nomksale"+j] = isMk ?  0 : d["sale"+j] ; 

					d['sale'+copY] += d["sale"+j] ;
					d['cost'+copY] += d["cost"+j] ; 


					res.sum['sale'+j]+= d["sale"+j];

					res.sum['cost'+j] += d["cost" + j ];

					res.sum['mksale'+j] += d["mksale"+j] ;
					res.sum['nomksale'+j] += d["nomksale"+j] ;

					res.sum['sale'+copY] += d["sale"+j];
					res.sum['cost'+copY] += d["cost"+j];
					res.sum['mksale'+copY] += d["mksale"+j];
					res.sum['nomksale'+copY] += d["nomksale"+j];


				}


				d.rito = d.proincome == null || d.proincome == 0 ? 0 : d.estimatedSales / d.proincome * 100 ;


				res.data.push(d);


			}
			res.rito = mksale / sumsale * 100 ;
		    return  res ; 
		}


